﻿using System;
using System.Linq;
using UniMana.Utils.Controls;
using UniMana.Utils.Configs;

namespace UniMana.Data
{
    public class Training_ScoreInfo
    {
        #region /*Không thay đổi*/
        public bds m_bds = new bds();
        private bool m_bExist = false;
        public void Save()
        {
            this.m_bds.EndEdit();
            m_data.m_DCSave.SubmitChanges();
        }
        public void Delete()
        {
            m_bds.DeleteCurrent();
        }
        #endregion /*Không thay đổi*/
        private DBMain m_data = new DBMain();
        #region [Properties - Thuộc tính]
        public Training_Score Properties
        {
            get { return m_bds.Current as Training_Score; }
        }
        #endregion
        public Training_ScoreInfo(string studentKey = DefaultConfig.EMPTY_GUID, int subjectClassKey = 0, int scoreTypeKey = 0)
        {
            var IQuery = m_data.m_DCSave.Training_Scores.Where(tb => tb.Training_StudentKey == Guid.Parse(studentKey) &&
                                                                tb.Training_Subject_ClassKey == subjectClassKey &&
                                                                tb.Training_ScoreTypeKey == scoreTypeKey);
            m_bds.DataSource = IQuery;
            var Row = IQuery.FirstOrDefault();
            this.m_bExist = true;
            if (Row == null)
            {
                this.m_bExist = false;
                Row = new Training_Score();
                //Row.Training_StudentKey =Guid.Parse(studentKey);
                m_bds.Add(Row);
            }
        }
        /// <summary>
        /// Lấy tổng số tín chỉ
        /// </summary>
        /// <param name="subjectClass">Mã lớp học phần</param>
        //public double TotalCredit(int subjectClass, string studentID)
        //    {
        //        if (subjectClass != 0 && studentID.ToString() != "" && studentID !="")
        //        {
        //            var c = from t in m_data.m_DCReadOnly.Training_Students
        //                    join t0 in m_data.m_DCReadOnly.Training_Student_Classes on t.Training_StudentKey equals t0.Training_StudentKey
        //                    join t1 in m_data.m_DCReadOnly.Training_TrainingPrograms
        //                        on new { t0.Catalogue_MajorKey, t0.Catalogue_TrainingTypeKey, t0.Catalogue_LevelKey, t0.Catalogue_CourseKey }
        //                        equals new { Catalogue_MajorKey = Convert.ToInt32(t1.Catalogue_MajorKey), t1.Catalogue_TrainingTypeKey, t1.Catalogue_LevelKey, t1.Catalogue_CourseKey }
        //                    join t2 in m_data.m_DCReadOnly.Training_TrainingProgram_Details on t1.Training_TrainingProgramKey equals t2.Training_TrainingProgramKey
        //                    join t3 in m_data.m_DCReadOnly.Training_Subject_Classes on new { Training_SubjectKey = t2.Training_SubjectKey } equals new { Training_SubjectKey = Convert.ToInt32(t3.Training_SubjectKey) }
        //                    where
        //                      t0.ID == studentID &&
        //                      t3.Training_Subject_ClassKey == subjectClass
        //                    select new
        //                    {
        //                        cr1 = t2.Credits1 == null ? 0 : t2.Credits1,
        //                        cr2 = t2.Credits2 == null ? 0 : t2.Credits2,
        //                        cr3 = t2.Credits3 == null ? 0 : t2.Credits3,
        //                        //Training_SubjectKey = (System.Int32?)t2.Training_SubjectKey,
        //                        t.Training_StudentKey,
        //                        t3.Training_Subject_ClassKey,
        //                        t0.ID
        //                    };
        //            double credits = 0;
        //            foreach (var cl in c)
        //            {
        //                credits = cl.cr1.Value + cl.cr2.Value + cl.cr3.Value;
        //            }
        //            return credits;
        //        }
        //        else
        //        {
        //            return 0;
        //        }
        //    }
        /// <summary>
        /// Lấy thông tin sinh viên theo mã sinh viên
        /// </summary>
        /// <param name="studentID">Mã sinh viên</param>
        /// <returns></returns>
        public IQueryable GetInfoStudentByID(string studentID)
        {
            var info = from s in m_data.m_DCReadOnly.Training_Students
                       join sc in m_data.m_DCReadOnly.Training_Student_Classes on s.Training_StudentKey equals sc.Training_StudentKey
                       join c in m_data.m_DCReadOnly.Training_Classes on sc.Training_ClassKey equals c.Training_ClassKey
                       join level in m_data.m_DCReadOnly.Catalogue_Levels on sc.Catalogue_LevelKey equals level.Catalogue_LevelKey
                       join major in m_data.m_DCReadOnly.Catalogue_Majors on sc.Catalogue_MajorKey equals major.Catalogue_MajorKey
                       join course in m_data.m_DCReadOnly.Catalogue_Courses on sc.Catalogue_CourseKey equals course.Catalogue_CourseKey
                       where sc.ID == studentID
                       where sc.Mainly == true
                       select new
                       {
                           ClassName = c.Name == null ? "" : c.Name,
                           Lastname = s.LastName == null ? "" : s.LastName,
                           Surename = s.SureName == null ? "" : s.SureName,
                           Birthday = s.Birthday == null ? s.ExceptionBirthday : s.Birthday.ToString(),
                           Gender = s.Gender == DefaultConfig.FEMALE?"Nữ":"Nam",
                           PlacesofBirth = s.PlaceOfBirth == null ? null : s.PlaceOfBirth,
                           Level = level.Name,
                           Course = course.Name,
                           Major = major.Name,
                           sc.ID
                       };
            return info;
        }
        public bool ExistInDatabase
        {
            get { return this.m_bExist; }
        }
        public IQueryable GetScoreInfoTranscript(int subjectClassKey)
        {
            var info = from subjectClass in m_data.m_DCReadOnly.Training_Subject_Classes
                       from schoolYear in m_data.m_DCReadOnly.Catalogue_SchoolYears
                       from term in m_data.m_DCReadOnly.Catalogue_Terms
                       from subject in m_data.m_DCReadOnly.Training_Subjects
                       from personel in m_data.m_DCReadOnly.Personnel_Personnels
                       from department in m_data.m_DCReadOnly.Catalogue_Departments
                       //from tClass in m_data.m_DCReadOnly.Training_Classes
                       //from major in m_data.m_DCReadOnly.Catalogue_Majors
                       //from level in m_data.m_DCReadOnly.Catalogue_Levels

                       where subjectClass.Catalogue_SchoolYearKey == schoolYear.Catalogue_SchoolYearKey
                       where subjectClass.Catalogue_TermKey == term.Catalogue_TermKey
                       where subjectClass.Training_SubjectKey == subject.Training_SubjectKey
                       where subjectClass.Personnel_PersonnelKey == personel.Personnel_PersonnelKey
                       where subject.Catalogue_DepartmentKey == department.Catalogue_DepartmentKey
                       //where subjectClass.Training_ClassKey == tClass.Training_ClassKey
                       //where tClass.Catalogue_LevelKey == level.Catalogue_LevelKey
                       //where tClass.Catalogue_MajorKey == major.Catalogue_MajorKey

                       select new
                       {
                           subjectClass.Training_Subject_ClassKey,
                           subjectClassID = subjectClass.ID,
                           subjectClassName = subjectClass.Name,
                           schoolYearName = schoolYear.Name,
                           termName = term.Name,
                           departmentName = department.Name,
                           subjectName = subject.Name,
                           subject.TotalCredits,
                           personelName = personel.LastName +" "+ personel.Name
                           //levelName = level.Name,
                           //majorName = major.Name
                       };
            return info;
        }
    }
}
